﻿#! python3
# !/usr/bin/python
# -*- coding: UTF-8 -*-

from shutil import copyfile
from sys import exit
from file_ext import file_name_make

import os

# 源文件和目标文件路径，前缀每个‘.’代表目录上移一级
src_dir = ["/Objects", "/Objects_Dry", "/Objects_Dry_Glass"]

# 要删除的文件类型列表，
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# KEIL 
file_type = ["o", "crf", "d", "htm"]
# IAR  file_type = ["dep","pbi","o","xcl"]
# KEIL & IAR  file_type = ["o", "crf", "d", "htm", "dep", "pbi", "xcl"]
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<
# >>>操作需谨慎<<<


# 删除指定后缀名，指定创建文件超过N天的文件
def del_file(dir_in):
    # 文件名列表
    fl_list = os.listdir(dir_in)
    # 删除的文件个数# 获取指定目录下的所有文件名
    fl_amount = 0
    for i in range(len(fl_list)):
        namelist = fl_list[i].split(".")
        # 获取后缀
        suffix = namelist[len(namelist) - 1]
        # 文件的路径 + 名字
        filename = dir_in + "/" + fl_list[i]
        # 类型检测
        for type_check in file_type:
            if suffix == type_check:
                os.remove(filename)
                fl_amount += 1
                print("删除的文件：" + filename)
                break
    print("删除文件数量 ： %d" % fl_amount)
    return


# 获取当前路径
path_base = os.getcwd()

# 删除
for path_now in src_dir:
    # 创建路径字符串
    path_src = file_name_make(path_base, path_now)
    path_st = False

    try:
        path_st = os.path.isdir(path_src)
    except IOError:
        print("文件路径错误 :" + path_src)

    if path_st:
        del_file(path_src)
    else:
        print("不是文件夹 :" + path_src)

input("entry key to quit!")

exit(0)
